package com.cs171.market.mapper;

import com.cs171.market.entity.Attribute;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.io.StringWriter;
import java.util.List;

/**
 * @author Endy
 * @create 2020 - 06 - 19 - 17:36
 * @date 2020/6/19 - 17:36
 */

@Mapper
public interface AttributeMapper {
    @Select("Select attr_id, attr_name, cat_id, attr_sel, attr_vals " +
            "FROM sp_attribute WHERE cat_id=#{id} and attr_sel=#{sel}")
    List<Attribute> getAttributeByIdAndSel(Integer id, String sel);

    @Select("Insert INTO sp_attribute (attr_name, cat_id, attr_sel, attr_vals)" +
            " VALUES(#{name},#{id},#{sel},#{val})")
    void setAttributeById(String name, Integer id, String sel, String val);

    @Select("Select * FROM sp_attribute WHERE attr_id=(SELECT MAX(attr_id) FROM sp_attribute )")
    Attribute getNewAttribute();

    @Select("Delete FROM sp_attribute WHERE cat_id=#{id} and attr_id=#{attrid}")
    void deleteAttribute(Integer id, Integer attrid);

    @Select("Select * FROM sp_attribute WHERE cat_id = #{id} and attr_id = #{attrid} and attr_sel = #{sel}")
    Attribute getAttributeByIdAndAttridAndSel(Integer id, Integer attrid, String sel);

    @Select("Update sp_attribute SET attr_name = #{name}, attr_vals = #{val} WHERE  cat_id = #{id} and attr_id = #{attrid} and attr_sel = #{sel}")
    void updateAttribute(Integer id, Integer attrid, String name, String sel, String val);

    @Select("Select attr_sel FROM sp_attribute WHERE attr_id = #{id}")
    String getSelById(Integer id);

    @Select("Update sp_attribute SET attr_sel = 'many' WHERE attr_id = #{id}")
    void changeSelById(Integer id);

    @Select("Select attr_vals FROM sp_attribute WHERE attr_id = #{id}")
    String getValsById(Integer id);

    @Select("Update sp_attribute SET attr_vals=#{vals} WHERE attr_id = #{id}")
    void setValsById(Integer id, String vals);

    @Select("Select attr_id, attr_name, attr_sel, attr_vals FROM sp_attribute WHERE attr_id = #{id}")
    Attribute getAttributeByIdForGood(Integer id);
}
